<?php


class Model {

    //数据库信息
    protected static $link=null;

    private $table=null;

    //初始化表信息
    private $opt;


    public function __construct($table=null){
        $this->table=C("DB_PREFIX") . $table;
        self::_connect();
        $this->_opt();
    }


    private  function _opt(){
        $this->opt=[
            "field" =>"*",
            "where" =>"",
            "order"=>"",
            "group"  =>"",
            "having" =>"",
            "limit" =>"",
            "data"  =>[]
        ];
    }

    private static function _connect(){
        if(is_null(self::$link)){
            $link=new mysqli(C("DB_HOST"),C("DB_USER"),C("DB_PASSWORD"),C("DB_DATABASE"));
            // 检查连接
            if ($link->connect_error) {
                die("连接失败: " . $link->connect_error);
            }
            $link->set_charset("utf8mb4");
            self::$link=$link;
        }
    }

    public function data($data){
        $this->opt['data']=$data;
        return $this;
    }

    public function order($order){
        $this->opt['order']=" ORDER BY ".$order;
        return $this;
    }

    public function add($data=NULL){
        if(is_null($data)){
            $data=$this->opt['data'];
        }
        $fields="";
        $values="";
        foreach ($data as $k=>$v){
            $fields.=" `".$k . "`,";
            $values.=" '".$v . "',";
        }
        $fields=rtrim($fields,",");
        $values=rtrim($values,",");
        @$sql = "INSERT INTO ". $this->table . " (".$fields.") ".VALUES." (".$values.") ";
        $link=self::$link;
        $link->query($sql);
        if($link->insert_id > 0){
            return $link->insert_id;
        }else{
            echo "Error: " . $sql . "<br>" . $link->error;
        }
        $link->close();
    }

    /**
     * @param $sql
     * @return mixed
     */
    public function query($sql){
       $link=self::$link;
       $result =$link->query($sql);
       if($link->error){
           die($link->error);
       }
       $rows=array();
       while ($row=$result->fetch_assoc()){
           $rows[]=$row;
       }
       $this->_opt();
       $link->close();
       return  $rows;
    }


    public function field($field="*"){
        $this->opt['field']=$field;
        return $this;
    }

    public function limit($limit){
        $this->opt['limit']=" limit ".$limit;
        return $this;
    }

    public function where($where){
        $this->opt['where']=" where ".$where;
        return $this;
    }

    public function find($id=null) : array{
        if(is_null($id)){
            if(empty($this->opt['where'])){
                $res=$this->all()[0];
            }else
            $res=$this->all()[0];
        }else{
            $this->where("id=" . $id);
            $res=$this->all()[0];
        }
        return $res;
    }

    public function select(){
        return $this->all();
    }

    public function all(){
        $sql="SELECT ".$this->opt['field'] . " FROM ".$this->table . $this->opt['where'] . $this->opt['order'] . $this->opt['group'] . $this->opt['having'] . $this->opt['limit'];
        return $result  = $this->query($sql);
    }
}